<?php

namespace App\Http\Controllers\Admin\Sys;

use App\Http\Common\Constant;
use App\Http\Models\Admin\Sys\Contacts;
use App\Http\Models\Admin\File\FileContent;
use App\Http\Responses\ResponseDto;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\View;

class FileEditController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response{cate}/{subcategory}
     */
    public function index(Request $request)
    {


//        $hostdir=dirname(__FILE__);

        //获取本文件目录的文件夹地址


        $files =  File::allFiles(base_path()."/resources/views/customer");

        return View::make('admin.sys.file_edit')
            ->withFiles($files)
            ->withAssets($request['ASSETS']);
    }

    public function getfile(Request $request){

        $url =base_path()."/resources/views/customer/".$request->input('file');
        if(File::exists($url)){
            return json_encode(File::get($url));
        }
        return "";
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
       $path =  base_path()."/resources/views/customer/".$request->input('file');
       $value = $request->input('value');
        if(File::exists($path)){

            if(!File::isWritable($path)){
                return ResponseDto::createResObj(trans("admin/message.permission_denied"),ResponseDto::$FAIL);
            };
            File::put($path, $value);
            return ResponseDto::createResObj(trans("admin/message.submit_success"),ResponseDto::$SUCCESS);
        }

        return ResponseDto::createResObj(trans("admin/message.update_fail"),ResponseDto::$FAIL);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //

        //
        Contacts::destroy($id);
        return ResponseDto::createResObj(trans("admin/message.submit_success"),ResponseDto::$SUCCESS);

    }
}
